package com.yuliang.utils.util;

import lombok.extern.slf4j.Slf4j;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author admin
 */
@Slf4j
public class StringUtils extends org.apache.commons.lang3.StringUtils{
    private final static int NUM_32 = 32;
    /**
     * 通过该字段生成唯一的订单号
     * */
    private static int machineId = 1;
    private static final Pattern CAMLE_PATTERN = Pattern.compile("_(\\w)");
    private static final Pattern UNDER_LINE_PATTEN = Pattern.compile("[A-Z]");

    /**
     * 下划线转驼峰
     * */
    public static StringBuffer underlineToHump(StringBuffer stringBuffer){
        Matcher matcher = CAMLE_PATTERN.matcher(stringBuffer);
        StringBuffer sb = new StringBuffer(stringBuffer);
        if(matcher.find()){
            sb = new StringBuffer();
            matcher.appendReplacement(sb, matcher.group(1).toLowerCase());
            //将之后的添加到StringBuffer对象中
            matcher.appendTail(sb);
        }else{
            return sb;
        }
        return underlineToHump(sb);
    }
    public static String format(String template, Object... params) {
        if (isEmptyObject(params) || isEmpty(template)) {
            return template;
        }
        return StrFormatter.format(template, params);
    }
    /**
     * 驼峰命名转下划线
     * */
    public static StringBuffer humpToUnderLine(StringBuffer str){
        Matcher matcher = UNDER_LINE_PATTEN.matcher(str);
        StringBuffer sb = new StringBuffer(str);
        if(matcher.find()){
            sb = new StringBuffer();
            //将当前匹配的子串替换为指定的字符串，并且将替换后的子串以及之前到上次匹配子串之后的字符串字段添加到一个StringBuffer重
            matcher.appendReplacement(sb, "_"+matcher.group(0).toLowerCase());
            matcher.appendTail(sb);

        }else{
            return sb;
        }
        return humpToUnderLine(sb);
    }
    /**
     * 校验uid是否满足限定的32位
     * */
    public static boolean checkUidList(Collection<String> collection){
        if(collection.size() == 0){
            return false;
        }
        for(String uid : collection){
            if(uid.trim().length() != NUM_32){
                return false;
            }
        }
        return true;
    }
    /**
     * String字符串转化为long
     * */
    public static long getLong(String str, Long defaultData){
        long lnum = defaultData;
        if (isEmpty(str)){
            return lnum;
        }
        try{
            lnum = Long.valueOf(str.trim()).longValue();
        }catch (NumberFormatException e){
            e.printStackTrace();
        }
        return lnum;
    }
    /**
     * 判断字符串是否为空
     * */
    public static boolean isEmpty(String str) {
        if(str == null || str.length() == 0){
            return true;
        }else{
            return false;
        }
    }
    public static boolean isEmptyObject(Object object){
        if(object == null){
            return true;
        }
        return false;
    }

    /**
     * 判断字符串是否为非空
     * */
    public static boolean isNotEmpty(String str){
        return !StringUtils.isEmpty(str);
    }
    /**
     * 按照code截取字符串
     * */
    public static String[] split(String str, String code){
        String[] split;
        if(isEmpty(str)) {
            split = null;
        }else{
            split = str.split(code);
        }
        return split;
    }
    /**
     * 把字符串按照code转化为 list<String>
     * */
    public static List<String> changeStringToString(String str, String code){
        String[] split = split(str,code);
        List<String> stringList = new ArrayList<>();
        for(String s : split){
            stringList.add(s);
        }
        return stringList;
    }
    /**
     * 判断是否为空字符串
     * */
    public static boolean isBlack(String str) {
        int strLen;
        if(str == null || (strLen = str.length()) ==0){
            return true;
        }
        for(int i=0; i<strLen; i++){
            if(!Character.isWhitespace(str.charAt(i))){
                return false;
            }
        }
        return true;
    }
    /**
     *把字符串暗转code转换为 List<Integer>
     * */
    public static List<Integer> changeStringToLong(String str, String code){
        String[] split = split(str, code);
        List<Integer> inums = new ArrayList<>();
        for(String s : split){
            inums.add(getInt(s,0));

        }
        return inums;
    }
    /**
     * 判断一个字符串是否为数字
     */
    public static boolean isNumeric(String str){
        try{
            Integer.valueOf(str);
            return true;
        }catch(Exception e){
            return false;
        }
    }
    /**
     * 判断list集合是否为空
     * */
    public static Boolean isEmptyList(List<String> list){
        if(list.size() <= 0 || list == null){
            return true;
        } else{
            return false;
        }
    }
    /**
     *判断某某字符是否在字符串中
     */
    public static boolean contains(String str, String searchChar){
        if(isEmpty(str)){
            return false;
        }
        return str.indexOf(searchChar) >= 0;
    }
    /**
     * 切割字符串
     * */
    public static String subString(String str,int start){
        if (isEmpty(str)){
            return null;
        }
        if (start < 0){
            start = str.length() +start;
        }
        if (start > str.length()){
            return null;
        }
        return str.substring(start);
    }
    /**
     * 把String类型转化为Integer类型
     */
    private static Integer getInt(String str, int defaultData) {
        int inum = defaultData;
        if(isEmpty(str)){
            return inum;
        }
        try{
            inum = Integer.valueOf(str.trim()).intValue();
        }catch (NumberFormatException numberFormatException){
            numberFormatException.printStackTrace();
        }
        return inum;
    }
    /**
     * String转化为Double
     */
    private static Double getDouble(String str, Double defaultData) {
        double dnum = defaultData;
        if(isEmpty(str)){
            return dnum;
        }
        try{
            dnum = Double.valueOf(str.trim()).doubleValue();
        }catch (NumberFormatException e){
            e.printStackTrace();
        }
        return dnum;
    }
    /**
     * 生成唯一用户订单uuid
     */
    public static String getOrderNumberByUUID(){
        int hashCode = UUID.randomUUID().toString().hashCode();
        if(hashCode < 0){
            hashCode = -hashCode;
        }
        //%010d判断字符串长度，不够15位，在前面补0
        String orderNumber = DateUtils.getDateByString().replaceAll("-", "") + String.format("%010d", hashCode);
        return orderNumber;
    }
    /**
     * 获取UUID
     * */
    public static String getUUID() {
        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        return uuid;
    }
    /**
     * 获取雪花id
     */
    public static String getSnowFlakeId(){
        SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker(0, 0);
        return String.valueOf(snowflakeIdWorker.nextId());
    }

    /**
     * 获取随机字符串
     */
    public static String getRandomNumStr(int number){
        StringBuffer stringBuffer = new StringBuffer();
        String source = "0123456789";
        while(StringUtils.isEmpty(stringBuffer.toString())){
            Random rd = new Random();
            for (int i=0; i < number; i++) {
                int pos = rd.nextInt(source.length());
                String s = source.substring(pos, pos + 1);
                stringBuffer.append(s);
            }
        }
        return stringBuffer.toString();
    }

    public static String getRandomStr(int number){
        StringBuffer stringBuffer = new StringBuffer();
        String source = "qwertyuioplkjhgfdsazxcvbnmQAZWSXEDCRFVTGBYHNUJMIKLOP0123456789_";
        while(StringUtils.isEmpty(stringBuffer.toString())){
            Random rd = new Random();
            for (int i=0; i < number; i++) {
                int pos = rd.nextInt(source.length());
                String s = source.substring(pos, pos + 1);
                stringBuffer.append(s);
            }
        }
        return stringBuffer.toString();
    }

    public static String removeBlack(String str){
        String newStr = str.replaceAll(" ", "");
        return newStr;
    }

    /**
     * 校验是否为空,如果为空，则打印异常，如果不为空，则返回对象
     * */
    public <T> T checkNotNull(T object, String text){
        if(object == null){
            throw new NullPointerException(text);
        }
        return object;
    }

    public static void main(String[]args){
        System.out.println(getOrderNumberByUUID());
    }

}